<?php
$path="C:/xampp/htdocs/00_JO/JOi/";
include_once( $path."DataProvider/DataProvider.php" );
include_once( $path."JO_02_function.php"		);

init();

?>

<?php

	function init(){
		if(isset($_POST["SO_id"])&& $_POST["SO_query"]=="delete_comment"){
			JO_comment_updatedel();
                        
			JO_location("../index.php?SO_unit=08_comment");
		}
		if(isset($_POST["SO_id"])&& $_POST["SO_query"]=="approval_comment"){
			JO_comment_updateapp();
			JO_location("../index.php?SO_unit=08_comment");
			
			
		}
		if(isset($_POST["SO_id"])&& $_POST["SO_query"]=="comment_update"){
			JO_comment_update();
			JO_location("../index.php?SO_unit=08_comment");
			
			
		}
	}

	/*
	 * ============================================== Get List of Topic ======================================
	 * Author			: Nguyem Minh Tuan
	 * Created Date		: 23/08/2011
	 * Modified Date	: 23/08/2011
	 */

	/*
	 * Get list of topic with name of parent field
	 */
	 function test(){
		$s_SQLCommand = "call test();";
		$data=array();
		$rs_result = DataProvider::executeSQL($s_SQLCommand);
		if($rs_result!=NULL){
		while($rows=$rs_result->fetch_array()){
			array_push($data,$rows);
		}
		}
		return $data; 
	}
	$result=test();
	$_S="";
	function getchild($arr,$key){	/*lay ra những topic con của 1,2		*/						
            $_S="";
            return getchild_($arr,$key,$_S);
		
	}
        function getchild_($arr,$key,$_S){	/*lay ra những topic con của 1,2		*/
            for($i=0;$i<count($arr);$i++){
			if($arr[$i][1]==$key){
				$_S.=getchild($arr,$arr[$i][0]);
				}
		}
		return $key.";".$_S;

	}
	function JO_kpopnews_getlist($topic){
		$result=test();
		$kq=";".getchild($result,$topic).";";	
		$s_SQLCommand = "call JO_news_getlist('$kq');";
	
		$rs_result = DataProvider::executeSQL($s_SQLCommand);
		return $rs_result; 
	}	
	function JO_kpopnews_getpagelist($vt,$limit,$topic){
		$result=test();
		$kq=";".getchild($result,$topic).";";	
		$s_SQLCommand = "call JO_kpopnews_getpagelist($vt,$limit,'$kq');";
		
		$rs_result = DataProvider::executeSQL($s_SQLCommand);
		return $rs_result; 
	}	
	function JO_news_relate_getlist($topic,$tag,$key){
		$result=test();
		$kq=";".getchild($result,$topic).";";	
		$s_SQLCommand = "call JO_news_relate_getlist('$kq','$tag','$key')";
		$rs_result = DataProvider::executeSQL($s_SQLCommand);
		return $rs_result; 
	}
	function JO_newsComment_getlist(){
		$s_SQLCommand = "call JO_newsComment_getlist()";
		$rs_result = DataProvider::executeSQL($s_SQLCommand);
		return $rs_result; 
	}
	function JO_comment_update(){
		$key=$_POST['SO_chude'];
		$content=$_POST['content'];
		$s_SQLCommand = "call JO_comment_update('$content',$key)";
		
		$rs_result = DataProvider::executeNoneSQL($s_SQLCommand);
		if($rs_result>0)
		return 1;
		return 0;
		//JO_location();
	}
	function JO_comment_updateapp(){
		$key=$_POST['SO_id'];
		$approval=($_POST['chk_approval'])?1:0;
		$s_SQLCommand = "call JO_comment_updateapp($approval,$key)";
		$rs_result = DataProvider::executeNoneSQL($s_SQLCommand);
		if($rs_result>0)
		return 1;
		return 0;
		//JO_location();
	}
	function JO_comment_getpagelist($vt,$limit){
		$s_SQLCommand = "call JO_comment_getpagelist($vt,$limit);";
		$rs_result = DataProvider::executeSQL($s_SQLCommand);
		return $rs_result; 
	}
	function JO_comment_updatedel(){
		$key=$_POST['SO_id'];
		$s_SQLCommand = "call JO_comment_updatedel($key)";
		$rs_result = DataProvider::executeNoneSQL($s_SQLCommand);
	}
	###########################################################
	function JO_article_getpagelist($vt,$limit){
		$s_SQLCommand = "call JO_article_getpagelist($vt,$limit);";
		$rs_result = DataProvider::executeSQL($s_SQLCommand);
		return $rs_result; 
	}

	function JO_article_gettopic($parent_id){
		$s_SQLCommand = "call JO_article_gettopic('$parent_id');";
		$rs_result = DataProvider::executeSQL($s_SQLCommand);
		return $rs_result; 
	}
	function JO_article_serchchild($key){
		$s_SQLCommand = "call JO_article_searchchild('$key')";
		$rs_result = DataProvider::executeSQL($s_SQLCommand);
		return $rs_result; 
	}
	function JO_article_updatedel(){
		$key=$_POST['chk_delete'];
		
		foreach($key as $v=>$k)
		{
			//$id=strstr($k,"|");
			//$id=substr($id,1);
			//$sub=strstr($k,"|",true);
			//$result=JO_article_serchchild($sub);
			//$result=$result->num_rows;
			//if($result==0)
			//{
				$s_SQLCommand = "call JO_article_updatedel($k)";
				$rs_result = DataProvider::executeNoneSQL($s_SQLCommand);
				
			//}
			//else
			//{
			//	return 0;
			//}
		}
	}
	
		
		
	
	/*
	 * Add a topic
	 */
	function JO_article_insert(){
		$topic=htmlentities($_POST['cb_topic']);
		$title=htmlentities($_POST['txt_title']);
		$description=htmlentities($_POST['ta_description']);
		$content=htmlentities($_POST['ta_content']);
		$image=htmlentities($_POST['txt_image']);
		$approval=($_POST['chk_approval'])?0:1;
		$s_SQLCommand = "call JO_article_insert('$topic','$title','$description','$content','$image',$approval);";
		$rs_result = DataProvider::executeNoneSQL($s_SQLCommand);
		if($rs_result>0)
		return 1;
		return 0;
		//JO_location();
	}
	function JO_article_update(){
		$key=$_POST['SO_chude'];
		$topic=htmlentities($_POST['cb_topic']);
		$title=htmlentities($_POST['txt_title']);
		$description=htmlentities($_POST['ta_description']);
		$content=htmlentities($_POST['ta_content']);
		$image=htmlentities($_POST['txt_image']);
		$approval=($_POST['chk_approval'])?0:1;
		$s_SQLCommand = "call JO_article_update('$topic','$title','$description','$content','$image',$approval,$key)";
		
		$rs_result = DataProvider::executeNoneSQL($s_SQLCommand);
		if($rs_result>0)
		return 1;
		return 0;
		//JO_location();
	}
	
?>